/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.data.crud;

import br.data.model.Listaprecos;
import br.data.model.ListasPK;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.taglibs.standard.lang.jstl.test.beans.Factory;

/**
 *
 * @author thiago
 */
public class CrudListaPrecos {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("wabtPU");

    public void persist(Object object) {
        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    public java.util.Collection<br.data.model.Listaprecos> getAll(ListasPK listaPk) {
        emf.getCache().evictAll();
        EntityManager em = emf.createEntityManager();
        try {
            return em.createQuery("SELECT l FROM Listaprecos l WHERE l.listaprecosPK.codigolist = :codigolist AND l.listaprecosPK.codigocli = :codigocli").setParameter("codigocli", listaPk.getCodigocli()).setParameter("codigolist", listaPk.getCodigolist()).getResultList();
        } catch (Exception e) {
            return null;
        } finally {
            em.close();
        }
    }

    public java.util.Collection<br.data.model.Listaprecos> getAll(int codList, int codForn, int codCli) {
        emf.getCache().evictAll();
        EntityManager em = emf.createEntityManager();
        try {
            return em.createQuery("SELECT l FROM Listaprecos l WHERE l.listaprecosPK.codigolist = :codigolist AND l.listaprecosPK.codigocli =:codigocli AND l.listaprecosPK.codigoforn = :codigoforn ").setParameter("codigocli", codCli).setParameter("codigolist", codList).setParameter("codigoforn", codForn).getResultList();
        } catch (Exception e) {
            return null;
        } finally {
            em.close();
        }
    }

    public java.util.Collection<br.data.model.Listaprecos> getRanking(int codList, int codCli) {
        EntityManager em = emf.createEntityManager();
        try {
            Collection<br.data.model.Listaprecos> lista = em.createQuery("SELECT l FROM Listaprecos l WHERE l.listaprecosPK.codigolist = :codigolist AND l.listaprecosPK.codigocli =:codigocli ORDER BY l.valorunitario, l.listaprecosPK.codigoprod").setParameter("codigocli", codCli).setParameter("codigolist", codList).getResultList();

            ArrayList<br.data.model.Listaprecos> result = new ArrayList<Listaprecos>();

            for (br.data.model.Listaprecos preco : lista) {
                boolean controle = false;
                for (br.data.model.Listaprecos tmp : result) {
                    if (!result.isEmpty() && (tmp.getListaprecosPK().getCodigoprod() == preco.getListaprecosPK().getCodigoprod())) {
                        controle = true;
                    }
                }
                if (!controle) {
                    result.add(preco);
                }
            }

            return result;
        } catch (Exception e) {
            return null;
        } finally {
            em.close();
        }
    }

    public java.util.ArrayList<br.data.model.Listaprecos> getDistinctFornecedores(ListasPK listaPk) {
        emf.getCache().evictAll();
        EntityManager em = emf.createEntityManager();
        try {
            Collection<br.data.model.Listaprecos> lista = em.createQuery("SELECT l FROM Listaprecos l WHERE l.listaprecosPK.codigolist = :codigolist AND l.listaprecosPK.codigocli = :codigocli").setParameter("codigocli", listaPk.getCodigocli()).setParameter("codigolist", listaPk.getCodigolist()).getResultList();
            ArrayList<br.data.model.Listaprecos> result = new ArrayList<Listaprecos>();
            if (!lista.isEmpty()) {
                for (br.data.model.Listaprecos preco : lista) {
                    boolean controle = false;
                    for (br.data.model.Listaprecos tmp : result) {
                        if (!result.isEmpty() && (tmp.getFornecedores() == preco.getFornecedores())) {
                            controle = true;
                        }
                    }
                    if (!controle) {
                        result.add(preco);
                    }
                }
            }
            return result;
        } catch (Exception e) {
            return null;
        } finally {
            em.close();
        }
    }

    public java.util.ArrayList<br.data.model.Listaprecos> getDistinctListas(int codigoForn) {
        emf.getCache().evictAll();
        EntityManager em = emf.createEntityManager();
        try {
            Collection<br.data.model.Listaprecos> lista = em.createQuery("SELECT l FROM Listaprecos l WHERE l.listaprecosPK.codigoforn = :codigoforn").setParameter("codigoforn", codigoForn).getResultList();

            ArrayList<br.data.model.Listaprecos> result = new ArrayList<Listaprecos>();

            for (br.data.model.Listaprecos preco : lista) {
                boolean controle = false;
                for (br.data.model.Listaprecos tmp : result) {
                    if (!result.isEmpty() && ((tmp.getListaprecosPK().getCodigolist() == preco.getListaprecosPK().getCodigolist()) && tmp.getListaprecosPK().getCodigocli() == preco.getListaprecosPK().getCodigocli())) {
                        controle = true;
                    }
                }
                if (!controle) {
                    result.add(preco);
                }
            }

            return result;
        } catch (Exception e) {
            return null;
        } finally {
            em.close();
        }
    }
}
